﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace TakeoutMangesDAL
{
    public class DBHelper
    {
        private static readonly string Constr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sql">sql命令</param>
        /// <param name="type">sql类型</param>
        /// <param name="pams">sql参数</param>
        /// <returns>查询结果</returns>
        public static SqlDataReader ExecuteReader(string sqlstr, CommandType type = CommandType.Text, params SqlParameter[] pams)
        {
            //1、声明数据库链接
            SqlConnection conn = new SqlConnection(Constr);
            //2、声明命令
            using (SqlCommand cmd = new SqlCommand(sqlstr, conn))
            {
                //添加参数
                if (pams != null && pams.Length > 0) 
                {
                    cmd.Parameters.AddRange(pams);
                }
                   cmd.CommandType = type;   
                //3、打开数据库链接
                if (conn.State == ConnectionState.Closed) 
                {
                    conn.Open();
                }
                    
                //执行存储过程
              
                //4、执行命令
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                //cmd.Parameters.Clear();
                return reader;
            }
        }
        /// <summary>
        /// 执行查询_返回第一条数据
        /// </summary>
        /// <param name="sql">sql命令</param>
        /// <param name="type">sql类型</param>
        /// <param name="pams">sql参数</param>
        /// <returns>查询结果</returns>
        public static object ExecuteScalar(string sqlstr, CommandType type = CommandType.Text, params SqlParameter[] pams)
        {
            //1、声明数据库链接
            SqlConnection conn = new SqlConnection(Constr);
            //2、声明命令
            using (SqlCommand cmd = new SqlCommand(sqlstr, conn))
            {
                //添加参数
                if (pams != null && pams.Length > 0)
                    cmd.Parameters.AddRange(pams);
                //3、打开数据库链接
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                //执行存储过程
                cmd.CommandType = type;
                //4、执行命令
                object obj = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return obj;
            };
        }
        //增删改操作
        public static int ExecuteNonQuery(string sqlstr, params SqlParameter[] pams)
        {
            //1.声明数据库连接
            using (SqlConnection conn = new SqlConnection(Constr))
            {
                //2.声明命令
                using (SqlCommand cmd = new SqlCommand(sqlstr, conn))
                {
                    //添加参数
                    if (pams != null && pams.Length > 0)
                    {
                        cmd.Parameters.AddRange(pams);
                    }
                    //3.打开数据库连接
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    int r = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    //4.执行命令
                    return r;
                }
            }
        }
        /// <summary>
        /// 返回DataTable类型的数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static DataTable ExecuteTable(string sql, params SqlParameter[] pams)
        {
            //1.声明SqlDataAdapter对象
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, Constr))
            {
                //2.添加参数
                if (pams != null && pams.Length > 0)
                {
                    sda.SelectCommand.Parameters.AddRange(pams);
                }
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }

        }
    }
}
